package com.js.nickcar.module.addcar.model;

import android.text.TextUtils;

import com.js.nickcar.api.HttpHelpter;
import com.js.nickcar.api.UrlConfig;
import com.js.nickcar.api.UrlUtils;
import com.js.nickcar.base.ApiCallback;
import com.js.nickcar.base.BaseRecyclerPresenter;
import com.js.nickcar.base.HttpResultFunc;
import com.js.nickcar.entity.remote.ApiResultBean;
import com.js.nickcar.entity.remote.CarModelBean;
import com.js.nickcar.entity.remote.CarModelYearBean;
import com.zyzd.commontools.log.TLog;

import java.util.ArrayList;
import java.util.Map;

import rx.Observable;

/**
 * Created by LZY on 2017/10/4.
 */
public class CarModePresenter extends BaseRecyclerPresenter<CarModelYearBean, CarModelView> {

    private String mCarSeriesId = "";

    public void setCarSeriesId(String carSeriesId) {
        mCarSeriesId = carSeriesId;
    }

    @Override
    public Observable<ApiResultBean<ArrayList<CarModelYearBean>>> getObservable() {
        Map<String, String> params = UrlUtils.getBaseRequestMap(UrlConfig.API_APPRAISER_CAR_MODEL_YEAR);
        params.put("car_series_id", TextUtils.isEmpty(mCarSeriesId) ? "" : mCarSeriesId);
        return HttpHelpter.getApiService().getCarModelYear(params);
    }

    /**
     * 请求具体车型数据
     *
     * @param carModelYearId
     * @param item
     * @param position
     */
    public void requestCarModelData(String carModelYearId, final CarModelYearBean item, final int position) {
        Map<String, String> params = UrlUtils.getBaseRequestMap(UrlConfig.API_APPRAISER_CAR_MODEL);
        params.put("car_model_year_id", carModelYearId);
        Observable<ArrayList<CarModelBean>> observable = HttpHelpter.getApiService().getCarModel(params)
                .map(new HttpResultFunc<ArrayList<CarModelBean>>());
        addSubscription(observable, new ApiCallback<ArrayList<CarModelBean>>() {
            @Override
            public void onSuccess(ArrayList<CarModelBean> model) {
                item.setModels(model);
            }

            @Override
            public void onFailure(String msg) {

            }

            @Override
            public void onFinish() {
                mView.notifyItemChanged(position);
            }
        });

    }
}
